//
//  HZUserManager.m
//  HZTC_YaDeArt
//
//  Created by MayerF on 17/3/25.
//  Copyright © 2017年 HZTC. All rights reserved.
//

#import "HZUserManager.h"
#import "HZKeyChain.h"

typedef void(^TimeBlock)();

@interface HZUserManager()
@property (nonatomic, strong)NSTimer *pTimer;
@property (nonatomic, copy)TimeBlock timeBlock;
@property (nonatomic, copy)NSString *pSwitchedIP;
@end

@implementation HZUserManager

+ (instancetype)manager
{
    static HZUserManager *pManager = nil;
    @synchronized (self) {
        static dispatch_once_t onceToken;
        dispatch_once(&onceToken, ^{
            pManager = [[HZUserManager alloc] init];
            
            [[NSNotificationCenter defaultCenter] addObserver:pManager selector:@selector(keyboardDidShow:) name:UIKeyboardDidShowNotification object:nil];
            [[NSNotificationCenter defaultCenter] addObserver:pManager selector:@selector(keyboardDidHide:) name:UIKeyboardDidHideNotification object:nil];
        });
    }
    
    return pManager;
}

+ (NSString *)getAPIHost
{
    NSString *host = kAPIHost;
    if ([HZUserManager manager].pSwitchedIP) {
        host = [HZUserManager manager].pSwitchedIP;
    }
    return host;
}
+ (void)swtichTempIPHost:(NSString *)host
{
    [HZUserManager manager].pSwitchedIP = host;
    [HZUserManager manager].isLogined = NO;
}

+ (BOOL)saveKeyChainData:(id)data key:(NSString *)key
{
    return [HZKeyChain keyChainSaveData:data keyName:key];
}
+ (id)getKeyChainDataWithkey:(NSString *)key
{
    return [HZKeyChain keyChainReadKeyName:key];
}
+ (BOOL)deleteKeyChainDataWithKey:(NSString *)key
{
    return [HZKeyChain keyChainDeletekeyName:key];
}

+ (void)saveUserDefaultData:(id)data key:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setObject:data forKey:key];
}
+ (id)getUserDefaultDataWithkey:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] objectForKey:key];
}
+ (void)deleteUserDefaultDataWithKey:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] removeObjectForKey:key];
}
+ (void)saveUserDefaultBoolValue:(BOOL)boolValue key:(NSString *)key
{
    [[NSUserDefaults standardUserDefaults] setBool:boolValue forKey:key];
}
+ (BOOL)getUserDefaultBoolValueWithkey:(NSString *)key
{
    return [[NSUserDefaults standardUserDefaults] boolForKey:key];
}

- (void)addTimerWithTime:(NSTimeInterval)time
                  repeat:(BOOL)isrepeat
               completed:(void(^)())completed
{
    [self removeTimer];
    NSTimer *timer = nil;
    if (ckSystemVersion >= 10.0) {
        timer = [NSTimer timerWithTimeInterval:time repeats:isrepeat block:completed];
    }else {
        timer = [NSTimer timerWithTimeInterval:time target:self selector:@selector(timerAction:) userInfo:nil repeats:isrepeat];
        _timeBlock = completed;
    }
    [[NSRunLoop mainRunLoop] addTimer:timer forMode:NSRunLoopCommonModes];
    [timer fire];
    _pTimer = timer;
}

- (void)removeTimer
{
    [_pTimer invalidate];
    _pTimer = nil;
    _timeBlock = nil;
}

- (void)timerAction:(NSTimer *)timer
{
    _timeBlock();
}

+ (void)handleCacheAccountAndPwd
{
    BOOL isInstall = [HZUserManager getUserDefaultBoolValueWithkey:kHZInstallAPPFlag];
    if (!isInstall) {//首次安装/卸载重装
        [HZUserManager saveUserDefaultBoolValue:YES key:kHZInstallAPPFlag];
        [HZUserManager deleteKeyChainDataWithKey:kHZUserKeyChainAccount];
        [HZUserManager deleteKeyChainDataWithKey:kHZUserKeyChainPwd];
    }
}

+ (void)logout
{
//    [HZUserManager deleteKeyChainDataWithKey:kHZUserKeyChainPwd];
    [HZUserManager manager].isLogined = NO;
    [HZUserManager manager].isDownLoadStock = NO;
    [HZUserManager manager].isVip = NO;
    [HZUserManager manager].nickName = nil;
    [HZUserManager manager].peiziAccountSession = nil;
    [HZUserManager manager].session = nil;
    [HZNetworkTool networkDeleteAllOwnStock];//清空本地自选股
}

#pragma mark -
#pragma mark notification
- (void)keyboardDidShow:(NSNotification *)noti
{
    _isKeyboardShow = YES;
}
- (void)keyboardDidHide:(NSNotification *)noti
{
    _isKeyboardShow = NO;
}

@end
